package com.example.administrator.shzs.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.shzs.R;
import com.example.administrator.shzs.entity.CollectShopData;
import com.example.administrator.shzs.entity.CollectShopData.ListBean;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;


/**
 * 首页 商家列表
 */
public class CollectShopListAdapter extends XRecyclerView.Adapter<CollectShopListAdapter.MyViewHolder> {

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private Context context;
    private List<ListBean> datalist;



    public CollectShopListAdapter(Context context, List<ListBean> datalist) {

        this.context = context;
        this.datalist = datalist;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.xrecyclerview_item, null);

        MyViewHolder viewHolderr = new MyViewHolder(view);


        return viewHolderr;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {


        final ListBean ListBean=datalist.get(position);

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.loding)
                .showImageForEmptyUri(R.mipmap.loding)
                .showImageOnFail(R.mipmap.loding)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        ImageLoader.getInstance().displayImage(datalist.get(position).getIcon(), holder.imageView, options);

        holder.tv_togo_name.setText(datalist.get(position).getTogoName());


        double sendMoney=Double.parseDouble(datalist.get(position).getSendmoney());
        int money=(int) sendMoney;
        holder.tv_delivery.setText(money+"");

        holder.tv_minute.setText(datalist.get(position).getSenttime()+"分钟");

        holder.tv_distance.setText(datalist.get(position).getDistance()+"km");

        if (datalist.get(position).getStatus() .equals("1")){
            holder.working.setVisibility(View.VISIBLE);
        }else {
            holder.working.setVisibility(View.GONE);
        }

        setListeners(holder, ListBean);

    }

    private void setListeners(final MyViewHolder holder, final ListBean listBean) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(mOnItemClickListener != null ){
                    mOnItemClickListener.onItemClick(listBean);
                }
            }
        });


        holder.shop_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener!= null){
                    mOnItemClickListener.onShopPayClick(listBean);

                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datalist == null ? 0 : datalist.size();
    }




    public class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;//头像
        private TextView tv_togo_name;//商家店名
        private ImageView iv_star1; //商家星级
        private TextView tv_sendmoney;//起送费
        private TextView tv_delivery;//配送费
        private TextView tv_distance;//距离公里
        private TextView tv_minute;//多少分钟到达
        private ImageView working;//是否营业
        public  ImageView shop_pay;


        public MyViewHolder(View itemView) {
            super(itemView);
            working = (ImageView) itemView.findViewById(R.id.working);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            tv_togo_name = (TextView) itemView.findViewById(R.id.tv_togo_name);
            shop_pay=(ImageView)itemView.findViewById(R.id.shop_pay);
            tv_delivery = (TextView) itemView.findViewById(R.id.tv_delivery);
            tv_minute= (TextView) itemView.findViewById(R.id.end_time);
            tv_distance= (TextView) itemView.findViewById(R.id.tv_gongli);

        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(ListBean datalist);
        void onShopPayClick(ListBean datalist);
    }

}
